/*
    SlideScript - minimalistic top-down scripting language.
    (C) Copyright 2014-2022 Chris Dorman - some rights reserved (GPLv2)
    View README file supplied with this software for more details
*/

//--------------------------------------------------------------------
// Header files.

#include "inc/deps.h"
#include "inc/vars.h"
#include "inc/util.h"

//--------------------------------------------------------------------
// Module-private variables and/or data.

static QLIST QM_VAR [ONE];

//--------------------------------------------------------------------
// Functions.

void release_vars (void)
{
    qflush (QM_VAR);
}

//--------------------------------------------------------------------

void set_var (int index, char *varname, char *vardata)
{
    strcpy (svars [index].var_name, varname);

    /* Set pointer in struct to a block of memory
       THEN write to the memory buffer. */

    svars [index].var_data = qmalloc (QM_VAR,
        strlen (vardata) + ONE);
    strcpy (svars [index].var_data, vardata);
}

//--------------------------------------------------------------------

char *get_var_data (char *varname)
{
    int xx;

    for(xx = ZERO; xx < MAXVARS; xx++)
    {
        char *varp = svars [xx].var_name;
        if  (*varp == '\0') continue;
        
        if(strcmp (varp, varname) == ZERO)
        {
            return svars [xx].var_data;
        }
    }

    return NULL;
}

//--------------------------------------------------------------------
// Clear variable data by name
//--------------------------------------------------------------------

char *clear_var_data (char *varname)
{
    int xx;

    for(xx = ZERO; xx < MAXVARS; xx++)
    {
        char *varp = svars [xx].var_name;
        if  (*varp == '\0') continue;

        if(strcmp (varp, varname) == ZERO)
        {
            qfree(svars[xx].var_data);
            strcpy(svars[xx].var_name, "\0");
            return "Variable free'd";
        }

    }

    return NULL;
}

//--------------------------------------------------------------------

int get_var_count()
{
    int yy;

    for(yy = ZERO; yy < MAXVARS; yy++)
    {
        if (strlen (svars [yy].var_name) > ZERO)
        {
            continue;
        }
        else
        {
            return yy;
        }
    }

    return ZERO;
}

//--------------------------------------------------------------------

char *parse_vars (char *var_string)
{
    int str_char      ;
    int sbufsize      ;
    char *ptr_input   ;         // Pointer into input  text

    char *buf_output  ;         // Output              buffer
    char *ptr_output  ;         // Pointer into that   buffer

    char *buf_varname ;         // Variable-name       buffer
    char *ptr_varname ;         // Pointer into that   buffer

    char *var_data    ;         // Variable  data
    int   len_data    ;         // Length of data excluding EOS

//--------------------------------------------------------------------

    sbufsize     = strlen  (var_string) + ONE;
    buf_varname  = qmalloc (QM_VAR, sbufsize);
    buf_output   = qmalloc (QM_VAR, sbufsize);

    *buf_output  = NULLBYTE;
    *buf_varname = NULLBYTE;

//--------------------------------------------------------------------

    for (ptr_input = var_string, ptr_output = buf_output;
         (str_char = *ptr_input) != NULLBYTE;
         ptr_input++)
    {
        if (str_char != TOKEN)
        {
            *ptr_output++ = str_char ;
            *ptr_output   = NULLBYTE ;
            continue;
        }

        ptr_varname = buf_varname;
        do { *ptr_varname++ = (str_char = *++ptr_input); }
            while ((str_char != NULLBYTE) && (str_char != TOKEN));

        str_char = *ptr_input;
        if ((str_char != NULLBYTE) && (str_char != TOKEN))
            x_error ("ss:error:buffer out of sync");
        str_char = ptr_varname [-1];
        if ((str_char != NULLBYTE) && (str_char != TOKEN))
            x_error ("ss:error:buffer out of sync");

        ptr_varname [-1] = NULLBYTE;

        if (*buf_varname == NULLBYTE)
        {
            x_warn ("ss:warning:variable syntax error!");
            return NULL;
        }

        var_data = get_var_data (buf_varname);

        if (var_data == NULL)
        {
            x_error ("ss:error:variable data not found, abort!");
        }

        if (*ptr_input == NULLBYTE) break;

        len_data       =  strlen (var_data);
        int moo        =  ptr_output - buf_output;
        buf_output       =  qrealloc (buf_output,
            strlen (buf_output) + len_data + ONE);
        ptr_output =  buf_output + moo;

        strcat (buf_output, var_data);
        ptr_output += len_data;
    }

    *ptr_output = NULLBYTE;
    return buf_output;
}

//--------------------------------------------------------------------
// End of file.
